//
//  SLMP4Parser+Mdia.m
//  SLVideoParser
//
//  Created by S_LINE on 2020/6/19.
//  Copyright © 2020 S_LINE. All rights reserved.
//

#import "SLMP4Parser+Mdia.h"

#import "SLParserUtil.h"

@implementation SLMP4Parser (Mdia)

- (NSDictionary *)mdhdParser:(NSData *)data
{
    if (data.length < 1)
    {
        return nil;
    }
    NSString *versionKey = @"version";
    NSString *flagsKey = @"flags";
    NSString *timescaleKey = @"timescale";
    NSString *durationKey = @"duration";
    NSString *trackDurationKey = @"trackDuration";
    NSMutableDictionary *dict = [NSMutableDictionary new];
    NSData *versionData = [data subdataWithRange:NSMakeRange(0, 1)];
    uint64_t version = [SLParserUtil hexToDecimalWithData:versionData];
    [dict setObject:[NSNumber numberWithUnsignedLongLong:version] forKey:versionKey];
    uint64_t offset = 1;
    
    if (version == 0)
    {
        uint64_t flagsSize = 3;
        NSData *flagsData = [data subdataWithRange:NSMakeRange(offset, flagsSize)];
        uint64_t flags = [SLParserUtil hexToDecimalWithData:flagsData];
        offset += flagsSize;
        // creation time 创建时间（相对于UTC时间1904-01-01零点的秒数）
        uint64_t creationTimeSize = 4;
        offset += creationTimeSize;
        // modification time 修改时间
        uint64_t modificationTimeSize = 4;
        offset += modificationTimeSize;
        // time scale 文件媒体在1秒时间内的刻度值，可以理解为1秒长度的时间单元数
        uint64_t timeScaleSize = 4;
        NSData *timeScaleData = [data subdataWithRange:NSMakeRange(offset, timeScaleSize)];
        uint64_t timeScale = [SLParserUtil hexToDecimalWithData:timeScaleData];
        offset += timeScaleSize;
        // duration track的时间长度
        uint64_t durationSize = 4;
        NSData *durationData = [data subdataWithRange:NSMakeRange(offset, durationSize)];
        uint64_t duration = [SLParserUtil hexToDecimalWithData:durationData];
        double trackDuration = (double)duration / (double)timeScale;
        offset += durationSize;
        // language 媒体语言码。最高位为0，后面15位为3个字符（见ISO 639-2/T标准中定义）
        uint64_t languageSize = 2;
        offset += languageSize;
        // pre-defined
        uint64_t preDefinedSize = 2;
        offset += preDefinedSize;
        
        [dict setObject:[NSNumber numberWithUnsignedLongLong:flags] forKey:flagsKey];
        [dict setObject:[NSNumber numberWithUnsignedLongLong:timeScale] forKey:timescaleKey];
        [dict setObject:[NSNumber numberWithUnsignedLongLong:duration] forKey:durationKey];
        [dict setObject:[NSNumber numberWithDouble:trackDuration] forKey:trackDurationKey];
    }
    
    return [dict copy];
}

- (NSDictionary *)hdlrParser:(NSData *)data
{
    if (data.length < 1)
    {
        return nil;
    }
    NSString *versionKey = @"version";
    NSString *flagsKey = @"flags";
    NSString *handlerTypeKey = @"handlerType";
    NSMutableDictionary *dict = [NSMutableDictionary new];
    NSData *versionData = [data subdataWithRange:NSMakeRange(0, 1)];
    uint64_t version = [SLParserUtil hexToDecimalWithData:versionData];
    [dict setObject:[NSNumber numberWithUnsignedLongLong:version] forKey:versionKey];
    uint64_t offset = 1;
    
    if (version == 0)
    {
        uint64_t flagsSize = 3;
        NSData *flagsData = [data subdataWithRange:NSMakeRange(offset, flagsSize)];
        uint64_t flags = [SLParserUtil hexToDecimalWithData:flagsData];
        offset += flagsSize;
        // pre-defined
        uint64_t preDefinedSize = 4;
        offset += preDefinedSize;
        // handler type 在media box中，该值为4个字符：“vide”— video track, “soun”— audio track, “hint”— hint track
        uint64_t handlerTypeSize = 4;
        NSData *handlerTypeData = [data subdataWithRange:NSMakeRange(offset, handlerTypeSize)];
        NSString *handlerType = [SLParserUtil hexToStringTypeWithData:handlerTypeData];
        offset += preDefinedSize;
        // reserved 保留位
        uint64_t reservedSize = 12;
        offset += reservedSize;
        // name 长度不定
        
        [dict setObject:[NSNumber numberWithUnsignedLongLong:flags] forKey:flagsKey];
        [dict setObject:handlerType forKey:handlerTypeKey];
    }
    
    return [dict copy];
    
}

@end
